/**
 *Copyright(c) HNA SYSTEMS Co., LTD
 *
 *@date 2013-7-18
 * 	
 *Original Author: 郑兴(zhengxing)
 *
 *ChangeLog
 *
 */
package com.hnas.sys.core.spring;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.springframework.beans.factory.config.PropertyOverrideConfigurer;

import sun.misc.BASE64Decoder;

/**
 * 
 * @author 郑兴(zhengxing)
 *
 */
public class EncryptedPropertyOverrideConfigurer extends
		PropertyOverrideConfigurer {
	/*
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.config.PropertyResourceConfigurer#convertPropertyValue(java.lang.String)
	 */
	protected String convertPropertyValue(String originalValue) {
		return decrypt(originalValue);
	}
	
	/**
	 * <pre>解密</pre>
	 * @Auther 郑兴(zhengxing)
	 * @param originalValue
	 * @return
	 */
	private String decrypt(String originalValue) {
		try {
			return decode(originalValue);
		} catch (Exception e) {
			throw new RuntimeException("decode original value error.",e);
		}
	}
	
	public static String decode(String secret) throws NoSuchPaddingException,
			NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
			IllegalBlockSizeException,UnsupportedEncodingException {
		byte[] kbytes = "jaas is the way".getBytes("UTF-8");
		SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");

		BigInteger n = new BigInteger(secret, 16);
		byte[] encoding = n.toByteArray();

		Cipher cipher = Cipher.getInstance("Blowfish");
		cipher.init(Cipher.DECRYPT_MODE, key);
		byte[] decode = cipher.doFinal(encoding);
		return new String(decode,"UTF-8");
	}

	public static String encode(String secret) throws NoSuchPaddingException,
			NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
			IllegalBlockSizeException,UnsupportedEncodingException {
		byte[] kbytes = "jaas is the way".getBytes("UTF-8");
		SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");

		Cipher cipher = Cipher.getInstance("Blowfish");
		cipher.init(Cipher.ENCRYPT_MODE, key);
		byte[] encoding = cipher.doFinal(secret.getBytes("UTF-8"));
		BigInteger n = new BigInteger(encoding);
		return n.toString(16);
	}
	
	public static void main(String[] args) throws Exception {
//		String org = "jdbc:mysql://127.0.0.1:3306/trip?useUnicode=true&characterEncoding=utf-8";
//		String org = "jdbc:mysql://127.0.0.1:3306/trip?useUnicode=true&characterEncoding=utf-8";
//		String org = "s9-qEYoz9";
//		String ss="-21e92dc6190fffb02ecb34b08c0e0e03";
		String ssDecode=decode("-7a44c481b56cf393");
		System.out.println(ssDecode);
		String encode = encode("hnt_dev");
		System.out.println(encode);
//		String password="promotion";
//		String password="123456";
//		String passwordStr = encode(password);
//		System.out.println(passwordStr);
		
//		String ssMail="d3N5MTk3NTEx";
//		BASE64Decoder encoder=new BASE64Decoder();	
//		String pass =  (new String(encoder.decodeBuffer(ssMail)));
//		//String ssMailStr=decode(ssMail);
//		System.out.println(pass);
		

	}
}